#include "DropParse.h"

void DropParse::render()
{
	m_txt.drawBLD();
	if(m_texture.m_texture.m_texId!=-1)
		m_texture.draw();
}

void DropParse::release()
{
	SAFE_RELEASE_LIST(m_text);
	m_txt.release();
	//m_txt.release();
}

int DropParse::parse(char* a_file)
{

	switch(extension(a_file))
	{
	case TEXTURE:
		m_texture.initTexture(m_core,a_file,TEX_CENTER,V2DF(640,360),V2D_ONES,0);
		m_texture.update();
		break;
	case TXT:
		FILE * pFile;
		fopen_s(&pFile,a_file,"r");

		if(pFile == NULL)
			return -1;
		char temp;
		char* newLine;
		newLine = new char[128];
		ZeroMemory(newLine,128);

		do {
			temp = fgetc (pFile);
			if(temp == '\n')
			{
				m_text.add(newLine);
				console::getInstance().addString(newLine);
				newLine = new char[128];
				ZeroMemory(newLine,128);
			}
			else if(temp == EOF)
			{
				m_text.add(newLine);
				console::getInstance().addString(newLine);
			}
			else
			{
				sprintf_s(newLine,128,"%s%c",newLine,temp);
			}

			if(newLine[0] == EOF)
				delete newLine;

		} while (temp != EOF);

		if(pFile != NULL)
			fclose(pFile);
		break;
	case MODEL:
	case AUDIO:
	case VIDEO:
	default:
		break;
	}

}

int DropParse::extension(char* a_file)
{
	bool hasExt = false;
	char* ext = new char[MAX_PATH];
	ZeroMemory(ext, MAX_PATH);
	for (int i = 0; i < strlen(a_file); i++)
	{
		if(hasExt)
			sprintf_s(ext,MAX_PATH,"%s%c",ext,a_file[i]);
		if(a_file[i] == '.')
		{	
			ZeroMemory(ext, MAX_PATH);
			hasExt = true;
		}
		if(a_file[i] == '\0')
			break;
	}
	if(!strcmp(ext,"txt")||!strcmp(ext,"h")||!strcmp(ext,"cpp")||!strcmp(ext,"hpp")||
		!strcmp(ext,"map"))
	{
		delete [] ext;
		return TXT;
	}
	if(!strcmp(ext,"png")||!strcmp(ext,"jpg")||!strcmp(ext,"gif")||!strcmp(ext,"jpeg")||
		!strcmp(ext,"bmp")||!strcmp(ext,"PNG")||!strcmp(ext,"dds"))
	{
		delete [] ext;
		return TEXTURE;
	}
}